package com.nandi.deviceinstall.presenter

import android.util.Log
import com.nandi.deviceinstall.base.BasePresenter
import com.nandi.deviceinstall.bean.*
import com.nandi.deviceinstall.contract.EquimentStepContract
import com.nandi.deviceinstall.contract.LoginContract
import com.nandi.deviceinstall.contract.TapRecordContract
import com.nandi.deviceinstall.contract.UploadSuccessEquipmentContract
import com.nandi.deviceinstall.model.EquipmentStepModel
import com.nandi.deviceinstall.model.LoginModel
import com.nandi.deviceinstall.model.TapRecordModel
import com.nandi.deviceinstall.model.UploadSuccessEquipmentModel
import com.nandi.deviceinstall.net.BaseSubscriber
import com.nandi.deviceinstall.net.RxScheduler
import com.uber.autodispose.FlowableSubscribeProxy
import org.reactivestreams.Subscription


class TapRecordPresenter : BasePresenter<TapRecordContract.View>(), TapRecordContract.Presenter {

    private val model: TapRecordContract.Model

    init {
        model = TapRecordModel()
    }

    override fun requestDeleteTapRecord(token: String, id: Int) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeDeleteTapRecord(token,id)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DeleteBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DeleteBean>(mView!!) {
                    override fun onNext(t: DeleteBean) {
                        mView!!.handlerDeleteTap(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestDisNum(token: String, disName: String, areaCode: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeDisNum(token,disName,areaCode)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DisNumBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DisNumBean>(mView!!) {
                    override fun onNext(t: DisNumBean) {
                        mView!!.handlerDisNum(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListDisaster(token: String, current: Int, size: Int, areaCode: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListDisaster(token,current,size,areaCode)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DisasterBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DisasterBean>(mView!!) {
                    override fun onNext(t: DisasterBean) {
                        mView!!.handlerListDisasterResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListTown(token: String, id: Int) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListTown(token, id)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<TownBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<TownBean>(mView!!) {
                    override fun onNext(t: TownBean) {
                        mView!!.handlerListTownResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }



    override fun requestListCity(token: String, district: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListCity(token, district)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<CityBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<CityBean>(mView!!) {
                    override fun onNext(t: CityBean) {
                        mView!!.handlerListCityResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListRecord(token: String, current: Int, size: Int, monitorMethod: Int, areaCode: String,
                                   sort: Int, townCode: String, disasterName: String, disNo: String,
                                   monitorNo: String, startTime: String, endTime: String, isAppAdd: Int) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListRecord(token, current, size, monitorMethod, areaCode, sort, townCode, disasterName,
                disNo, monitorNo, startTime, endTime, isAppAdd)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<TapRecordBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<TapRecordBean>(mView!!) {
                    override fun onNext(t: TapRecordBean) {
                        mView!!.handlerListRecordResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun responseListRecordResult(result: TapRecordBean) {

    }

    override fun responseListCityResult(result: CityBean) {

    }

    override fun requestDisName(token: String, disasterName: String, town: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeListDisName(token, disasterName,town)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DisNameBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DisNameBean>(mView!!) {
                    override fun onNext(t: DisNameBean) {
                        mView!!.handlerDisNameResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListMonitorNum(token: String, unifiedCode: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListMonitorNum(token,unifiedCode)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<ListMonitorNumBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<ListMonitorNumBean>(mView!!) {
                    override fun onNext(t: ListMonitorNumBean) {
                        mView!!.handlerMonitorNum(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }
}

